import java.io.*;
import java.awt.image.BufferedImage;

import java.awt.*;

/*
 * Avatar.java
 *
 * Created on 4. november 2007, 17:24
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.net.URI;
import java.util.ArrayList;

/**
 *
 * @author Dark_Wolf
 */
public class FileAvatar  extends Avatar{
    
    
   
    
    protected File imageFile;
    public FileAvatar(File file,URI folder, String artist,String album,boolean gotImage,CDShelf shelf) {
        this.imageFile = file;
        this.folder =  folder;
        this.artist=artist;
        this.album=album;
        this.gotImage =  gotImage;
        
        this.shelf = shelf;
        fx = CrystalCaseFactory.getInstance();
        
    }
    
   
    
    
   
 
    
    public boolean loadImage() {
        
        if (!loaded) {
           
                try {
                    
                    image = fx.createReflectedPicture(fx.createCrystalCase(AlbumHelper.loadCompatibleImage(imageFile)));
                    loaded = true;
                    
                } catch(Exception e) {
                    
                    System.out.println("loadImageproblem: " + imageFile);
                    System.out.println(artist + "-" + album);
                    e.printStackTrace();
                }
            }
       
      
        return !loaded;
    }
 public void setImage(File newFile){
       
        
        String ending = newFile.getName().substring(newFile.getName().lastIndexOf(".")+1);
      
        
        imageFile = AlbumHelper.songbirdToJavaFile(folder.getScheme()+"://" +folder.getPath(), artist + "-" + album, ending);
       
        AlbumHelper.fileCopy(newFile, imageFile);
        
        if (shelf.getWriteMp3()){
                embedImage();      
        }
        if (loaded){
            unloadImage();
            loadImage();
        }
    }
    
     public void embedImage() {
        ArrayList<String> files = SqliteInterface.getInstance().getURIsForAlbum(album);
        for (String filePath : files) {
            System.out.println(filePath);
            if (filePath.substring(filePath.lastIndexOf(".")+1).equalsIgnoreCase("mp3")){
                File tempFile= AlbumHelper.songbirdToJavaFile(filePath);
                System.out.println("Mp3 found" + tempFile.isFile());
                ImageTagger.deleteImage(tempFile);
                ImageTagger.writeImage(imageFile,tempFile);
            }
            
        }
    }
    
    public BufferedImage getRawImage(){
         
                try {
                    
                    return AlbumHelper.loadCompatibleImage(imageFile);
                    
                    
                } catch(Exception e) {
                    
                    System.out.println("loadImageproblem: " + imageFile);
                    System.out.println(artist + "-" + album);
                    e.printStackTrace();
                    return null;
                }
            
    }
   
    
}



